﻿using KqAutoDesign.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace KqAutoDesign.SonViews
{
    /// <summary>
    /// AppSettingUc.xaml 的交互逻辑
    /// </summary>
    public partial class AppSettingUc : System.Windows.Controls.UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }
        public AppSettingUc()
        {
            InitializeComponent();
            DataContext = this;
        }

        public void SetData(ObservableCollection<RegistedApp> apps)
        {
            Apps = apps;
        }
        private ObservableCollection<RegistedApp> _apps ;
        public ObservableCollection<RegistedApp> Apps
        {
            get { return _apps; }
            set
            {
                if (_apps != value)
                {
                    _apps = value;
                    RaisePropertyChanged(nameof(Apps));
                }
            }
        }

        private RegistedApp _selectedApp;
        public RegistedApp SelectedApp
        {
            get { return _selectedApp; }
            set
            {
                if (_selectedApp != value)
                {
                    _selectedApp = value;
                    SelectedIcon = App.GlobalConfigs.Resources.Icons.FirstOrDefault(x => x.FileName == value.Icon);
                    AppName = value.Name;
                    Description = value.Description;
                    StartPath = value.StartPath;
                    RaisePropertyChanged(nameof(SelectedApp));
                }
            }
        }

        public GlobalConfigs GlobalConfigs { get { return App.GlobalConfigs; } }


        private IconInfo _selectedIcon;
        public IconInfo SelectedIcon
        {
            get { return _selectedIcon; }
            set
            {
                if(_selectedIcon != value)
                {
                    _selectedIcon = value;
                    if(SelectedApp!=null)
                    {
                        SelectedApp.Icon = _selectedIcon?.FileName;
                    }
                    RaisePropertyChanged(nameof(SelectedIcon));
                }
            }
        }
        private string _appName;
        public string AppName
        {
            get { return _appName; }
            set
            {
                if (value != _appName)
                {
                    _appName = value;
                    RaisePropertyChanged(nameof(AppName));
                }
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    RaisePropertyChanged(nameof(Description));
                }
            }
        }

        private string _startPath;
        public string StartPath
        {
            get { return _startPath; }
            set
            {
                if (_startPath != value)
                {
                    _startPath = value;
                    RaisePropertyChanged(nameof(StartPath));
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "可执行程序|*.exe;*.bat";
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                startPathTxt.Text = openFileDialog.FileName;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if(SelectedApp != null)
            {
                var res = System.Windows.MessageBox.Show("是否保存设置？", "请确认", MessageBoxButton.YesNo);
                if (res == MessageBoxResult.Yes)
                {
                    GlobalConfigXMLManager xmlManager = new GlobalConfigXMLManager();
                    var nodeString = string.Format("/App/RegistedApps/RegistedApp[@id='{0}']", SelectedApp.Id);
                    Dictionary<string, string> kvps = new Dictionary<string, string>();
                    kvps.Add("name", AppName);
                    kvps.Add("startPath", StartPath);
                    kvps.Add("icon", SelectedIcon.FileName);
                    kvps.Add("description", Description);
                    xmlManager.ModifyOneNodeAttributes(nodeString, kvps);
                    if(SelectedApp != null)
                    {
                        SelectedApp.Name = AppName;
                        SelectedApp.Description = Description;
                        SelectedApp.Icon = SelectedIcon.FileName;
                        SelectedApp.StartPath = StartPath;
                    }
                }
                else
                    return;
            }
        }

        private void addBtn_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(nameTxt.Text)||
               string.IsNullOrEmpty(startPathTxt.Text) ||
               SelectedIcon==null)
            {
                System.Windows.MessageBox.Show("请设置完整信息", "请确认", MessageBoxButton.OK);
                return;
            }
            if (Apps.FirstOrDefault(x => string.Compare(x.Name, nameTxt.Text.Trim(), true) == 0) != null||
                Apps.FirstOrDefault(x => string.Compare(x.StartPath, startPathTxt.Text.Trim(), true) == 0) != null)
            {
                System.Windows.MessageBox.Show("该应用已被添加", "请确认", MessageBoxButton.OK);
                return;
            }

            RegistedApp app = new RegistedApp();
            app.Id = Apps.Max(x => x.Id)+1;
            app.Name = nameTxt.Text.Trim();
            app.StartPath = startPathTxt.Text.Trim();
            app.Description = descTxt.Text.Trim();
            app.Icon = SelectedIcon.FileName;
            GlobalConfigXMLManager xmlManager = new GlobalConfigXMLManager();
            xmlManager.AddRegistedApp(app);
            Apps.Add(app);
        }
    }
}
